class WeeklyResult < ActiveRecord::Base
  establish_connection :weekly
  belongs_to :weekly

  #nil = 'new'
  #-1 = except
  #>0
  def last_score_rank
    return -1 if wid == weekly.last.last.wid
    last_weekly = weekly.last
    if last_weekly
      last_result = WeeklyResult.find_by_wid_and_weekly_id wid, last_weekly.id
      if last_result
        last_result.score_rank
      else
        last_except = WeeklyExcept.find_by_wid_and_weekly_id wid, last_weekly.id
        last_except ? -1 : nil
      end
    end
  end

  def is_changqi?
    corrent_results = WeeklyResult.find :all, :include => :weekly, :conditions => ["score_rank <= weeklies.rank_from and weekly_results.wid = ?", wid]
    corrent_results.size > 2 ? true : false
  end
end
